package dao;

import pojo.Student;
import utils.DataConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {

    public int addStudents(Student s) {
        int flag = 0;

        Connection conn = DataConn.getConn();
        String sql = "INSERT INTO student(sid, sname, ssex, sphone, sage) VALUES (?, ?, ?, ?, ?)";
//        String sql1 = "select *from student";

        List<Student> list = null;
        try (PreparedStatement pst = conn.prepareStatement(sql);
//             PreparedStatement pst1 = conn.prepareStatement(sql1)

        ) {

            // 直接使用传入的Student对象's'，而不是新建一个
            pst.setString(1, s.getSid());
            pst.setString(2, s.getSname());
            pst.setString(3, s.getSsex());
            pst.setString(4, s.getSphone());
            pst.setLong(5, s.getSage());
            flag = pst.executeUpdate();

            System.out.println("Executing SQL: " + sql);
//            list = new ArrayList<>();
//                ResultSet res = pst1.executeQuery();
//                while (res.next()) {
//                    Student student = new Student(res.getString("sid"),
//                            res.getString("sname"),
//                            res.getString("ssex"),
//                            res.getString("sphone"),
//                            res.getLong("sage"));
//                    list.add(student);
//
//                }



        } catch (SQLException e) {
            throw new RuntimeException("Error inserting student data", e);

        }
        return flag;
    }
}